package cn.picclife.mwx.manager.sys.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.picclife.mwx.manager.sys.mapper.SysTipMessageMapper;
import cn.picclife.mwx.manager.sys.mapper.SysTipsMatchMapper;
import cn.picclife.mwx.manager.sys.model.bo.DataResponse;
import cn.picclife.mwx.manager.sys.model.bo.MatchListReq;
import cn.picclife.mwx.manager.sys.model.bo.MatchQueryListReq;
import cn.picclife.mwx.manager.sys.model.bo.MatchSaveReq;
import cn.picclife.mwx.manager.sys.model.bo.MatchSwitchReq;
import cn.picclife.mwx.manager.sys.model.bo.TargetTipReq;
import cn.picclife.mwx.manager.sys.model.bo.TipQueryReq;
import cn.picclife.mwx.manager.sys.model.po.DateList;
import cn.picclife.mwx.manager.sys.model.po.QuerySysteamEntity;
import cn.picclife.mwx.manager.sys.model.po.SysTipMessageEntity;
import cn.picclife.mwx.manager.sys.model.po.SysTipsMatchEntity;
import cn.picclife.mwx.manager.sys.service.TipsMatchService;
import cn.picclife.mwx.manager.util.enums.ResponseStatusEnum;

/**
 * @description  系统与提示信息匹配service实现类
 * @author SaiZhiyi
 * @creatTime 2020年7月17日 上午10:41:57
 */
@Service
public class TipsMatchServiceimpl implements TipsMatchService {
	private static final Log log = LogFactory.getLog(TipsMatchServiceimpl.class);
	private static final String queryListCode = "000005"; 
	private static final String matchSaveCode = "000006"; 
	private static final String matchSwitchCode = "000007"; 
	private static final String queryMatchListCode = "000008"; 
	
	@Autowired
	private SysTipMessageMapper sysTipMessageMapper;
	
	@Autowired
	private SysTipsMatchMapper sysTipsMatchMapper;

	@Override
	public DataResponse queryList(MatchQueryListReq req) {
		DataResponse res = new DataResponse();
		log.info("[查询匹配信息列表]" + queryListCode + "======[开始]");
		DateList date = new DateList();
		int count = sysTipsMatchMapper.queryCount(req);
		if(count%req.getPageSize() == 0){
			date.setAllPage(count/req.getPageSize());
		}else{
			date.setAllPage(count/req.getPageSize() + 1);
		}
		date.setNowPage(req.getPageNo());
		List<SysTipsMatchEntity> list = sysTipsMatchMapper.queryList(req);
		if(list != null){
			date.setDate(list);
		}
		List<DateList> dlist = new ArrayList<DateList>();
		dlist.add(date);
		res.setData(dlist);
		res.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
		res.setMsg("查询成功");
		log.info("[查询匹配信息列表]" + queryListCode + "======[结束]");
		return res;
	}

	@Override
	public DataResponse matchSave(MatchSaveReq req) {
		DataResponse res = new DataResponse();
		log.info("[保存匹配信息]" + matchSaveCode + "======[开始]");
		TipQueryReq tipreq = new TipQueryReq();
		tipreq.setTipId(req.getTipId());
		List<SysTipMessageEntity> tip = sysTipMessageMapper.tipQuery(tipreq);
		SysTipMessageEntity tip0 = tip.get(0);
		SysTipsMatchEntity entity = new SysTipsMatchEntity();
		entity.setSystemCode(req.getSystemCode());
		entity.setSystemName(req.getSystemName());
		entity.setProductCode(tip0.getProductCode());
		entity.setProductName(tip0.getProductName());
		entity.setTipCode(tip0.getTipCode());
		entity.setTipVersion(tip0.getTipVersion());
		entity.setBusinessCode(tip0.getBusinessCode());
		entity.setTipMode(tip0.getTipMode());
		entity.setTipMessage(tip0.getTipMessage());
		entity.setSwitchFlag(req.getSwitchFlag());
		int num = sysTipsMatchMapper.matchSave(entity);
		log.info("保存条数" + num);
		if(num == 0){
			res.setCode(ResponseStatusEnum.UPDATE_FAIL_CODE.getCode());
			res.setMsg("您的保存的信息中(产品编码、提示编码、提示信息版本号)与其他提示信息重复");
		}else{
			res.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
			res.setMsg("新增成功");
		}
		log.info("[保存匹配信息]" + matchSaveCode + "======[结束]");
		return res;
	}

	@Override
	public DataResponse matchSwitch(MatchSwitchReq req) {
		DataResponse res = new DataResponse();
		log.info("[修改开关]" + matchSwitchCode + "======[开始]");
		if(req.getSwitchFlag().equals("1")){
			sysTipsMatchMapper.matchSwitchAll(req);
		}
		sysTipsMatchMapper.matchSwitch(req);
		res.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
		res.setMsg("修改成功");
		log.info("[修改开关]" + matchSwitchCode + "======[结束]");
		return res;
	}

	@Override
	public DataResponse queryMatchList(MatchListReq req) {
		DataResponse res = new DataResponse();
		log.info("[查询匹配数据列表]" + queryMatchListCode + "======[开始]");
		if(req.getFlag().equals("1") || req.getFlag().equals("2")){
			if(req.getFlag().equals("1")){
				req.setProductCode("");
			}
			List<SysTipMessageEntity> listfirst = sysTipMessageMapper.queryMatchTipsFirst(req);
			if(listfirst != null){
				res.setData(listfirst);
			}
		}else{
			List<SysTipMessageEntity> list = sysTipMessageMapper.queryMatchTips(req);
			if(list != null){
				res.setData(list);
			}
		}
		res.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
		res.setMsg("查询成功");
		log.info("[查询匹配数据列表]" + queryMatchListCode + "======[结束]");
		return res;
	}

	@Override
	public DataResponse querytargetTip(TargetTipReq req) {
		DataResponse res = new DataResponse();
		List<SysTipsMatchEntity> entity = sysTipsMatchMapper.queryTargetTip(req);
		if(entity != null){
			res.setData(entity);
		}
		res.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
		res.setMsg("查询成功");
		return res;
	}

	@Override
	public DataResponse querySysteam() {
		DataResponse res = new DataResponse();
		List<QuerySysteamEntity> list = sysTipsMatchMapper.querySysteam();
		if(list != null){
			res.setData(list);
		}
		res.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
		res.setMsg("查询成功");
		return res;
	}

}
